
***** Measure deterrence effects with synthetic controls

local StJoTreat = mofd(mdy(11,16,2004))
local StJoSettle = mofd(mdy(12,21,2007))

local WheatonTreat = mofd(mdy(10,19,2004))
local WheatonSettle = mofd(mdy(1,7,2010))

local ElCentroTreat = mofd(mdy(3,20,2006))
local ElCentroSettle = mofd(mdy(9,20,2010))

local OverlookTreat = mofd(mdy(4,25,2008))
local OverlookSettle = mofd(mdy(6,21,2012))

local MortonPlantTreat = mofd(mdy(8,27,2008)) 
local MortonPlantSettle = mofd(mdy(11,20,2012))

local ShandsTreat = mofd(mdy(8,30,2008))
local ShandsSettle = mofd(mdy(8,8,2013))

local chsTreat = mofd(mdy(1,7,2009))
local chsSettle= mofd(mdy(8,4,2014))

cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/feout

local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
foreach dft of local dftlist {

	cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/feout

	** Keep only defendant 
	use /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/feout/op_ipop_panel_feout_`dft'.dta, clear
	keep if prvdrnum == "`dft'"


	merge 1:1 dt using "synth_fit_`dft'.dta"

	local treatment_period = ``dft'Treat'


	** Produce difference in series
	gen deterrence = fit1 - out if dt >= `treatment_period'

	** Time discount the difference at 10% 
	local r = 1.1^(1/12)

	gen deterrence_disc = deterrence*1/(`r'^(dt-`treatment_period'))
	replace deterrence_disc = . if dt-`treatment_period' >= 60
	count if !mi(deterrence_disc)
	assert(r(N)==60)

	*** Compute total
	preserve
	collapse (sum) deterrence_disc
	qui summarize deterrence_disc
	local deterrence_`dft' = r(min)
	restore

	*** Truncate graph at 5 years 
	drop if dt  - `treatment_period' >= 60
	summarize dt 
	local minperiod = r(min)
	local maxperiod = r(max)

	** Find max Y 
	summarize fit1
	local ymax = 1.1*r(max)

	format dt %tmMon_YY
	twoway (scatter pmt_amt dt, mcolor(gs5)) (line out fit1 dt, lwidth(thick thick) lcolor(navy maroon)), xline(`treatment_period', lcolor(maroon)) xline(``dft'Settle', lcolor(black)) legend(order(2 "Defendant: `dft'" 3 "Synthetic Control") width(100)) xscale(range(`minperiod' `maxperiod')) xlabel(504(24)600 ``dft'Treat' ``dft'Settle') xtitle("Month") ytitle("$/Month") graphregion(color(white)) ylabel(#3) yscale(range(0 `ymax'))

	graph save "`dft'Overlay_feout.gph", replace
}


cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/feout

log using deterrence_feout.log, replace
local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
local total = 0 
foreach dft of local dftlist {
	disp "`dft' Deterrence:"
	disp `deterrence_`dft''
	local total = `total' + `deterrence_`dft''
}
disp `total'
log close 

***********************************
*** CHS Graph manual edits
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/feout
graph use chsOverlay_feout.gph
* Fix x and y axes and labels 


